package gui.panel_new.evaluation.professor;

import enums.ExportOptionsEnum;
import enums.FileFilterEnum;
import enums.TextBundleEnum;
import enums.URLEnum;
import gui.panel_new.OpenSavePanel;
import j_util.Utiles;
import misc.BundlesProvider;
import misc.ExportDataPreferences;
import model.MarksExportManager;
import model.eval.Test;
import net.Server;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Permite la visualizacion del test, abrir una conexion de servidor y establecer la duracion que ha de tener
 * la evaluacion;
 *
 * @author Jose Martin Rozanec;
 */
public class TestOverviewProfessorPanel extends OpenSavePanel {
    private JLabel selectedTestLabel;
    private JTextField selectedTestField;
    private JTextArea testOverviewArea;
    private JLabel ipLabel;
    private JTextField ipField;
    private JLabel studentsToEvaluateLabel;
    private JTextField studentsToEvaluateField;
    private JButton connectButton;
    private JTextArea connectedTextArea;
    private JScrollPane connectedAreaScrollPane;

    private JLabel durationLabel;
    private JLabel scoreLabel;
    private JTextField durationField;
    private JTextField scoreField;
    private JTextArea testArea;
    private JScrollPane testScrollPane;
    private JLabel minutesLabel;

    private JRadioButton nameSurnameRadio;
    private JRadioButton markRadio;
    private JCheckBox ascendenteCheckBox;
    private JCheckBox descendenteCheckBox;
    private JCheckBox plainTextCheckBox;
    private JCheckBox excel2003CheckBox;
    private JCheckBox excel2007CheckBox;
    private JTextField fileDirectoryField;
    private JButton searchFileDirectoryButton;
    private JButton addInstitutionButton;
    private JTextField institutionTextField;
    private JComboBox institutionsCombo;

    private JButton connectionPanelButton;

    private ResourceBundle textBundle;

    private Test test;
    private Server server;
    private JTabbedPane tabbedPane;
    private StudentMarksPanel studentsMarksPanel;

    private ExportDataPreferences exportPreferences;

    private static final String CONNECTION = "Connection";
    private static final String PREFERENCES = "Preferences";

    /**
     * Constructor;
     *
     * @param saveButton;
     * @param openButton;
     * @param fileChooser;
     * @param tabbedPane;
     * @param exportPreferences
     */
    public TestOverviewProfessorPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser,
                                      JTabbedPane tabbedPane, ExportDataPreferences exportPreferences) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        fileFilter = FileFilterEnum.TST;
        this.exportPreferences = exportPreferences;
        this.tabbedPane = tabbedPane;
        initComponents();
        layoutComponents();
        studentsMarksPanel = new StudentMarksPanel(36, exportPreferences, saveButton, openButton, fileChooser);
    }

    /**
     * Inicializa los componentes;
     */
    private void initComponents() {
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);

        selectedTestLabel = new JLabel(textBundle.getString(TextBundleEnum.SELECTED_TEST.toString()));
        selectedTestField = new JTextField(20);
        selectedTestField.setEnabled(false);
        testOverviewArea = new JTextArea(10, 50);
        testOverviewArea.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.LIGHT_GRAY), textBundle.getString(TextBundleEnum.TEST.toString())));

        ipLabel = new JLabel(textBundle.getString(TextBundleEnum.IP.toString()));
        ipField = new JTextField(15);
        studentsToEvaluateLabel = new JLabel(textBundle.getString(TextBundleEnum.STUDENTS_TO_BE_EVALUATED.toString()));
        studentsToEvaluateField = new JTextField(5);
        connectButton = new JButton(textBundle.getString(TextBundleEnum.START.toString()));
        connectedTextArea = new JTextArea(10, 50);
        connectedTextArea.setBackground(Color.WHITE);
        connectedAreaScrollPane = new JScrollPane(connectedTextArea);
        connectedAreaScrollPane.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEmptyBorder(), textBundle.getString(
                TextBundleEnum.CONNECTED_STUDENTS.toString())));

        durationLabel = new JLabel(textBundle.getString(TextBundleEnum.TEST_DURATION.toString()) + ":");
        minutesLabel = new JLabel(textBundle.getString(TextBundleEnum.MINUTES.toString()).toLowerCase());
        scoreLabel = new JLabel(textBundle.getString(TextBundleEnum.TOTAL_SCORE.toString()) + ":");
        durationField = new JTextField(5);
        scoreField = new JTextField(5);
        scoreField.setEditable(false);
        testArea = new JTextArea(17, 51);
        testArea.setEditable(false);
        testArea.setBackground(Color.WHITE);
        testScrollPane = new JScrollPane(testArea);

        nameSurnameRadio = new JRadioButton("Apellido y Nombre");
        markRadio = new JRadioButton("Nota de examen");
        ascendenteCheckBox = new JCheckBox("Ascendente");
        descendenteCheckBox = new JCheckBox("Descendente");
        plainTextCheckBox = new JCheckBox("Texto plano (.txt)");
        excel2003CheckBox = new JCheckBox("MSExcel 2003 (.xls)");
        excel2007CheckBox = new JCheckBox("MSExcel 2007 (.xlsx)");
        fileDirectoryField = new JTextField(25);
        searchFileDirectoryButton = new JButton("Examinar...");
        addInstitutionButton = new JButton("Agregar institucion");
        institutionTextField = new JTextField(17);
        institutionsCombo = new JComboBox(MarksExportManager.getInstitutionsList().toArray());

        ButtonGroup group1 = new ButtonGroup();
        ButtonGroup group2 = new ButtonGroup();
        ButtonGroup group3 = new ButtonGroup();
        nameSurnameRadio.setSelected(true);
        exportPreferences.setSortBy(ExportOptionsEnum.SORT_BY_NAME);
        nameSurnameRadio.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setSortBy(ExportOptionsEnum.SORT_BY_NAME);
            }
        });
        markRadio.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setSortBy(ExportOptionsEnum.SORT_BY_MARK);
            }
        });
        group1.add(nameSurnameRadio);
        group1.add(markRadio);
        ascendenteCheckBox.setSelected(true);
        exportPreferences.setSortAscendingDescending(ExportOptionsEnum.ASCENDING);
        ascendenteCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setSortAscendingDescending(ExportOptionsEnum.ASCENDING);
            }
        });
        descendenteCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setSortAscendingDescending(ExportOptionsEnum.DESCENDING);
            }
        });
        group2.add(ascendenteCheckBox);
        group2.add(descendenteCheckBox);

        excel2007CheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setExportAs(FileFilterEnum.XLSX);
            }
        });
        excel2003CheckBox.setSelected(true);
        exportPreferences.setExportAs(FileFilterEnum.XLS);
        excel2003CheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setExportAs(FileFilterEnum.XLS);
            }
        });
        plainTextCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportPreferences.setExportAs(FileFilterEnum.TXT);
            }
        });
        group3.add(excel2007CheckBox);
        group3.add(excel2003CheckBox);
        group3.add(plainTextCheckBox);

        fileDirectoryField.setText(System.getProperty("user.home"));//user.home
        fileDirectoryField.setEditable(false);
        searchFileDirectoryButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser filechooser = new JFileChooser();
                filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int accepted = filechooser.showOpenDialog(JOptionPane.getRootFrame());
                if (accepted == JFileChooser.APPROVE_OPTION) {
                    fileDirectoryField.setText(filechooser.getSelectedFile().getAbsolutePath());
                }
            }
        });
        addInstitutionButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String institution = institutionTextField.getText();
                if (institution.equals("")) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), textBundle.getString(TextBundleEnum.WARNING_EMPTY_FIELD.toString()));
                } else {
                    if (MarksExportManager.getInstitutionsList().contains(institution)) {
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                institution + " " + textBundle.getString(
                                        TextBundleEnum.WARNING_INSTITUTION_ALREADY_IN_LIST.toString()));
                    } else {
                        MarksExportManager.addInstitution(institution);
                        institutionsCombo.removeAllItems();
                        List<String> comboValues = MarksExportManager.getInstitutionsList();
                        for (int j = 0; j < comboValues.size(); j++) {
                            institutionsCombo.addItem(comboValues.get(j));
                        }
                        institutionTextField.setText("");
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                textBundle.getString(TextBundleEnum.WARNING_SUCCESFUL_ADDED_INSTITUTION.toString())
                                        + " " + institution);
                    }
                }
            }
        });
        assignActionListener();
        setEditables();
    }

    /**
     * Crea un panel con los campos de datos indispensables a ser completados para abrir una conexion de
     * servidor para iniciar la evaluacion;
     *
     * @return JPanel;
     */
    private JPanel getCompleteDataPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.add(Utiles.getLabelFieldLabelPanel(durationLabel, durationField, minutesLabel));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(new JLabel(Utiles.createImageIcon(URLEnum.SPACE_OP.toString())));
        panel.add(Utiles.getLabelFieldPanel(studentsToEvaluateLabel, studentsToEvaluateField));
        return panel;
    }

    /**
     * Crea un panel con el puntaje total que se puede lograr en la evaluacion y notificando del test seleccionado;
     *
     * @return
     */
    private JPanel getShowDataPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.add(Utiles.getLabelFieldPanel(selectedTestLabel, selectedTestField));
        panel.add(Utiles.getLabelFieldPanel(scoreLabel, scoreField));
        return panel;
    }

    /**
     * Permite realizar un layout de los componentes;
     */
    private void layoutComponents() {
        int y = 0;
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = 7;
        add(getShowDataPanel(), gridBagConstraints);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 5;
        add(getCompleteDataPanel(), gridBagConstraints);
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridx = 0;
        add(testScrollPane, gridBagConstraints);//testArea
        gridBagConstraints.gridy = ++y;
        add(createConnectionPanel(), gridBagConstraints);

        //panel de conexion IP, Iniciar || Conectados

        testScrollPane.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.LIGHT_GRAY), textBundle.getString(TextBundleEnum.TEST.toString())));
    }

    /**
     * Establece que componentes seran editables y cuales no;
     */
    private void setEditables() {
        ipField.setEditable(true);
        selectedTestField.setEditable(false);
        testOverviewArea.setEditable(false);
        connectedTextArea.setEditable(false);
    }

    /**
     * Crea un panel que posee los campos necesarios para ingresar los datos referentes a la creacion de una
     * conexion de servidor;
     *
     * @return JPanel;
     */
//    private JPanel createConnectionPanel() {
//        JPanel panel = new JPanel(new GridBagLayout());
//        panel.setBackground(Color.WHITE);
//        panel.setBorder(BorderFactory.createTitledBorder(
//                BorderFactory.createMatteBorder(1,1,0,0,Color.LIGHT_GRAY), textBundle.getString(TextBundleEnum.CONNECTION.toString())));
//        GridBagConstraints gridBagConstraints = new GridBagConstraints();
//        gridBagConstraints.gridwidth = 2;
//        panel.add(getConnectionAuxPanel(), gridBagConstraints);
//        gridBagConstraints.gridwidth = 3;
//        gridBagConstraints.gridy = 1;
//        gridBagConstraints.gridx = 0;
//        panel.add(connectedAreaScrollPane, gridBagConstraints);
//        return panel;
//    }
    private JPanel createConnectionPanel() {
        final JPanel stack = new JPanel(new CardLayout());

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createMatteBorder(1, 1, 0, 0, Color.LIGHT_GRAY), textBundle.getString(TextBundleEnum.CONNECTION.toString())));
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        panel.add(getConnectionAuxPanel(), gridBagConstraints);
        //todo
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new Insets(0, 205, 0, 0);
        JButton preferenciasButton = new JButton("Preferencias");
        preferenciasButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ((CardLayout) stack.getLayout()).show(stack, PREFERENCES);
            }
        });
        panel.add(preferenciasButton, gridBagConstraints);
        gridBagConstraints.insets = new Insets(0, 0, 0, 0);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        panel.add(connectedAreaScrollPane, gridBagConstraints);

        JPanel preferences = new JPanel(new GridBagLayout());
        preferences.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createMatteBorder(1, 1, 0, 0, Color.LIGHT_GRAY), textBundle.getString(TextBundleEnum.PREFERENCES.toString())));
        preferences.setBackground(Color.WHITE);

        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;

//        gridBagConstraints.insets = new Insets(0, 500, 0, 0);
        JButton connectionPanelButton = new JButton("Conexion");
        connectionPanelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ((CardLayout) stack.getLayout()).show(stack, CONNECTION);
            }
        });
        preferences.add(connectionPanelButton, gridBagConstraints);

        JPanel topButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        topButtonPanel.add(connectionPanelButton);
        topButtonPanel.setBackground(Color.WHITE);

        JPanel contentPanel = new JPanel(new GridLayout(6, 1));
        contentPanel.add(topButtonPanel);

        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new Insets(0, 0, 0, 0);
        gridBagConstraints.gridwidth = 2;
        JPanel datos = new JPanel(new FlowLayout(FlowLayout.LEFT));
        datos.setBackground(Color.WHITE);
        datos.add(new JLabel("Exportar datos de evaluacion ordenados por:"));
        datos.add(nameSurnameRadio);
        datos.add(markRadio);
//        preferences.add(new JRadioButton("Tiempo de entrega"),gridBagConstraints);
        contentPanel.add(datos);
//        preferences.add(datos, gridBagConstraints);
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 3;
        JPanel sorting = new JPanel(new FlowLayout(FlowLayout.LEFT));
        sorting.setBackground(Color.WHITE);
        sorting.add(new JLabel("Ordenar de modo"), gridBagConstraints);
        sorting.add(ascendenteCheckBox, gridBagConstraints);
        sorting.add(descendenteCheckBox, gridBagConstraints);
        contentPanel.add(sorting);
        contentPanel.setBackground(Color.WHITE);
//        preferences.add(sorting,gridBagConstraints);

        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        JPanel exportPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        exportPanel.add(new JLabel("Exportar a:"), gridBagConstraints);
//        exportPanel.add(excel2007CheckBox, gridBagConstraints);//se omite porque arroja error al exportar
        exportPanel.add(excel2003CheckBox, gridBagConstraints);
        exportPanel.add(plainTextCheckBox, gridBagConstraints);
        exportPanel.setBackground(Color.WHITE);
        contentPanel.add(exportPanel);
//        preferences.add(exportPanel,gridBagConstraints);

        gridBagConstraints.gridy = 4;
        JPanel filePath = new JPanel(new FlowLayout(FlowLayout.LEFT));
        filePath.add(new JLabel("Los archivos se guardaran en:"));
        filePath.add(fileDirectoryField);
        filePath.add(searchFileDirectoryButton);
        filePath.setBackground(Color.WHITE);
        contentPanel.add(filePath);
//        preferences.add(filePath,gridBagConstraints);
        //directorio/EvaluacionesLogike/Institucion/Evaluacion[anioMesDia]_apellidoNombre

        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        JPanel institution = new JPanel(new FlowLayout(FlowLayout.LEFT));
        institution.setBackground(Color.WHITE);
        institution.add(new JLabel("Institucion"));
        institution.add(institutionsCombo);
        institution.add(institutionTextField);
        institution.add(addInstitutionButton);
        contentPanel.add(institution);
//        preferences.add(institution, gridBagConstraints);

        gridBagConstraints.gridy = 0;
//        gridBagConstraints.gridwidth = 4;
        preferences.add(contentPanel, gridBagConstraints);

        contentPanel.setPreferredSize(new Dimension(560, 220));

        stack.add(CONNECTION, panel);
        stack.add(PREFERENCES, preferences);

        return stack;
    }

    /**
     * Metodo auxiliar, crea un panel con los label y field de IP, y boton de conexion;
     *
     * @return JPanel;
     */
    private JPanel getConnectionAuxPanel() {
        JPanel panel = new JPanel();
        panel.add(ipLabel);
        panel.add(ipField);
        panel.add(connectButton);
        panel.setBackground(Color.WHITE);
        return panel;
    }

    /**
     * Asigna los actionListeners a los diferentes componentes del panel;
     */
    private void assignActionListener() {
        connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!selectedTestField.getText().equals("")) {
                    if (!durationField.getText().equals("")) {
                        try {
                            int duration = Math.abs(Integer.parseInt(durationField.getText()));
                            if (!studentsToEvaluateField.getText().equals("")) {
                                try {
                                    //agrego conexiones de mas para que algun chistoso que se conecte no tire abajo el programa
                                    int connections = (int) Math.abs((Integer.parseInt(studentsToEvaluateField.getText()) * 1.5));
                                    test.setMinutes(duration);
                                    exportPreferences.setInstitution((String) institutionsCombo.getSelectedItem());
                                    exportPreferences.setDirectoryPath(Utiles.getPathFromHome(fileDirectoryField.getText()));
                                    exportPreferences.buildPath();
                                    server = new Server(connections, test, studentsMarksPanel, exportPreferences);
                                    server.setComponent(connectedTextArea);
                                    server.start();
                                    tabbedPane.addTab(textBundle.getString(TextBundleEnum.NOTAS_ALUMNOS.toString()), studentsMarksPanel);
                                    disablePreferencesComponents();
                                } catch (NumberFormatException nfe) {
                                    JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                                            textBundle.getString(TextBundleEnum.WARNING_CONEXIONES_ENTERO.toString()), "",
                                            JOptionPane.WARNING_MESSAGE);
                                }
                            } else {
                                JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                                        textBundle.getString(TextBundleEnum.WARNING_STUDENTS_TO_EVALUATE.toString()), "",
                                        JOptionPane.WARNING_MESSAGE);
                            }
                        } catch (NumberFormatException nfe) {
                            JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                                    textBundle.getString(TextBundleEnum.WARNING_DURACION_ENTERO.toString()), "",
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                                textBundle.getString(TextBundleEnum.WARNING_SPECIFY_DURATION.toString()), "",
                                JOptionPane.WARNING_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                            textBundle.getString(TextBundleEnum.WARNING_TEST_NOT_SELECTED.toString()), "",
                            JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }

    protected void setSaveActionListener() {
        class SaveTestAction extends AbstractAction {
            public SaveTestAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
            }
        }
        saveButton.setAction(new SaveTestAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(false);
    }

    protected void setOpenActionListener() {
        class OpenTestAction extends AbstractAction {
            public OpenTestAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    test = Utiles.deserialize(path);
                    scoreField.setText("" + test.getTotalScore());
                    if (test != null) {
                        selectedTestField.setText(path);
                        exportPreferences.setTestName((fileChooser.getSelectedFile().getName()).replace(".tst", ""));
                        setTest(test);
                    } else {
                        JOptionPane.showMessageDialog(TestOverviewProfessorPanel.this,
                                textBundle.getString(TextBundleEnum.WARNING_ERROR_ABRIR_ARCHIVO.toString()));
                    }
                }
            }
        }
        openButton.setAction(new OpenTestAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //se deja sin implementar
    }

    /**
     * Permite setaer un test;
     *
     * @param test;
     */
    public void setTest(Test test) {
        this.test = test;
        testArea.setText(test.toString());
        testArea.setCaretPosition(0);
    }

    private void disablePreferencesComponents() {
        nameSurnameRadio.setEnabled(false);
        markRadio.setEnabled(false);
        ascendenteCheckBox.setEnabled(false);
        descendenteCheckBox.setEnabled(false);
        plainTextCheckBox.setEnabled(false);
        excel2003CheckBox.setEnabled(false);
        excel2007CheckBox.setEnabled(false);
        fileDirectoryField.setEnabled(false);
        searchFileDirectoryButton.setEnabled(false);
        addInstitutionButton.setEnabled(false);
        institutionTextField.setEditable(false);
        institutionsCombo.setEnabled(false);
    }

    /**
     * Metodo usado solamente para acelerar los testeos. A ser removido, no tiene utilidad real;
     *
     * @deprecated;
     */
    public void auxSetConnectionValues() {
        ipField.setText("127.0.0.1");
        studentsToEvaluateField.setText("12");
        durationField.setText("12");

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        TestOverviewProfessorPanel panel = new TestOverviewProfessorPanel(new JButton("Save"), new JButton("New"),
                new JFileChooser(), new JTabbedPane(), new ExportDataPreferences());
        panel.auxSetConnectionValues();
        panel.setTest(new Test());
        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
